package logic

import (
	"context"
	"fmt"
	"phone/models/mysql"
	"phone/models/redis"
	"phone/phonerpc/internal/svc"
	"phone/phonerpc/phonerpc"
	"phone/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *phonerpc.RegisterRequest) (*phonerpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.PhoneUser
	mysql.DB.Where("account=?", in.Account).Find(&user)
	result, _ := redis.Rdb.Get(l.ctx, "graphic"+in.Account).Result()
	if result != in.Graphic {
		return nil, fmt.Errorf("图形验证错误")
	}
	sendsms, _ := redis.Rdb.Get(l.ctx, "sendsms"+in.Account).Result()
	if sendsms != in.SendSms {
		return nil, fmt.Errorf("短信验证错误")
	}
	users := mysql.PhoneUser{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	mysql.DB.Create(&users)
	return &phonerpc.RegisterResponse{
		Id: int64(users.ID),
	}, nil
}
